﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace EHTWebsite.Code
{
    /// <summary>
    /// Author : toanvt
    /// CreateDate : 04/02/2012
    /// Description : Provider cho Video
    /// </summary>
    public class VideoProvider
    {
        /// <summary>
        /// Author : toanvt
        /// CreateDate : 04/02/2012
        /// Description : Lấy danh sách dùng cho phần quản trị -> trả về danh sách Video
        /// </summary>
        public static DataSet GetVideosForBackEnd(Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetVideosForBackEnd";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));

            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;
        }

        /// <summary>
        /// Author : toanvt
        /// CreateDate : 04/02/2012
        /// Description : Lấy danh sách dùng cho phần front end -> trả về danh sách Video
        /// </summary>
        /// <param name="isVN">Ngông ngữ VN?</param>
        /// <returns></returns>
        public static DataSet GetVideosForFrontEnd(Boolean isVN)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetVideosForFrontEnd";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@IsVN", isVN));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            return ds;
        }

        public static Video GetVideoByVideoID(Int32 VideoID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetVideoByVideoID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@VideoID", VideoID));
            DataSet ds = DataProvider.ExecuteDataSet(command);
            Video theVideo = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theVideo = new Video();
                theVideo.VideoID = Int32.Parse(ds.Tables[0].Rows[0]["VideoID"].ToString());
                theVideo.VideoUrl = ds.Tables[0].Rows[0]["VideoUrl"].ToString();
                theVideo.MoTaVN = ds.Tables[0].Rows[0]["MoTaVN"].ToString();
                theVideo.MoTaOther = ds.Tables[0].Rows[0]["MoTaOther"].ToString();
                theVideo.DeleteFlag = Boolean.Parse(ds.Tables[0].Rows[0]["DeletedFlag"].ToString());
                theVideo.Publish = Boolean.Parse(ds.Tables[0].Rows[0]["Publish"].ToString());
                theVideo.NumberOfOrder = Int32.Parse(ds.Tables[0].Rows[0]["NumberOfOrder"].ToString());
            }

            return theVideo;
        }

        public static Int32 IUVideos(Video theVideo, Boolean isAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUVideos";
            command.CommandType = CommandType.StoredProcedure;
            SqlParameter theVideoIDIDParameter = new SqlParameter();

            theVideoIDIDParameter.ParameterName = "@VideoIDOut";
            theVideoIDIDParameter.Direction = ParameterDirection.Output;
            theVideoIDIDParameter.DbType = DbType.Int32;

            command.Parameters.Add(theVideoIDIDParameter);
            command.Parameters.Add(new SqlParameter("@VideoID", theVideo.VideoID));
            command.Parameters.Add(new SqlParameter("@VideoUrl", theVideo.VideoUrl));
            command.Parameters.Add(new SqlParameter("@MoTaVN", theVideo.MoTaVN));
            command.Parameters.Add(new SqlParameter("@MoTaOther", theVideo.MoTaOther));
            command.Parameters.Add(new SqlParameter("@Publish", theVideo.Publish));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", theVideo.NumberOfOrder));
            command.Parameters.Add(new SqlParameter("@IsAdd", isAdd));

            DataProvider.ExecuteNoneQuery(command);

            return Int32.Parse(command.Parameters["@VideoIDOut"].Value.ToString());
        }

        public static void DeleteVideoByVideoID(Int32 VideoID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteVideoByVideoID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@VideoID", VideoID));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static DataSet GetVideosWithPaging(Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetVideosWithPaging";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;
        }

        public static void ChangePublishByVideoID(Int32 videoID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_ChangePublishByVideoID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@VideoID", videoID));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static void UpdateNumberOfOrderByVideoID(Int32 videoID, Int32 numberOfOrder)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_UpdateNumberOfOrderByVideoID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@VideoID", videoID));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", numberOfOrder));
            DataProvider.ExecuteNoneQuery(command);
        }
    }

}
